import re

class Torrent:
	def __init__(self):
		self.edition = None
		self.url = None
		self.fileCount = None
		self.size = None
		self.time = None
		
		self.downloadLink = None
		self.informationLink = None
		self.formatInformation = None
		
		# Stats
		self.snatches = None
		self.seeders = None
		self.leechers = None
	
	def __string(self):
		format = ''
		if self.formatInformation is not None:
			format = self.formatInformation
		
		return '%s - %s (%s) (%s)' % (self.edition, format, self.size, getTimeString(self.time))
	
	def __str__(self):
		return self.__string()
	
	def __repr__(self):
		return self.__string()

def getTimeString(time):
	return time.strftime('%d %b %Y %I:%M:%S %p')

sizePattern = re.compile(r'([0-9]+\.[0-9]+) ([KMGT]?)B')
magnitudes = {'':0, 'K':3, 'M':6, 'G':9, 'T':12}

class Size:
	'''Used for representing a file storage amount'''
	def __init__(self, sizeString):
		match = re.match(sizePattern, sizeString)
		if match is None:
			self.valid = False
			self.stringMagnitude, self.stringSize, self.magnitude = None, None, None
		else:
			self.valid = True
			self.stringSize = match.groups()[0]
			self.stringMagnitude = match.groups()[1]
			self.magnitude = magnitudes[self.stringMagnitude]
	
	def getSize(self):
		return float(self.stringSize)
	
	def __string(self):
		if not self.valid:
			return '(invalid)'
		return '%s %sB' %(self.stringSize, self.stringMagnitude)
	
	def __str__(self):
		return self.__string()
	
	def __repr__(self):
		return self.__string()
			
class Edition:
	def __init__(self):
		self.name = None
		self.torrents = []
		self.album = None
		
		self.rawTorrentRows = []
	
	def __string(self):
		return self.name
	
	def __str__(self):
		return self.__string()
	
	def __repr__(self):
		return self.__string()

class Album:
	def __init__(self):
		self.editions = []
		self.name = None
		self.artists = None
		self.url = None
		self.year = None
	
	def __string(self):
		artists = '(unknown)'
		year = '----'
		if self.artists is not None:
			artists = ', '.join([x.name for x in self.artists])
		if self.year is not None:
			year = str(self.year)
		repr = '%s - %s (%s)' % (artists, self.name, year)
#		for edition in self.editions:
#			repr = repr + '\n\t' + str(edition) + '\n'
		return repr
	
	def __str__(self):
		return self.__string()
		
	def __repr__(self):
		return self.__string()
		
class Artist:
	def __init__(self):
		self.url = None
		self.name = None

class Restriction:
	def __init__(self, index, function):
		self.index = index
		self.function = function
	
	def apply(self, row):
		cell = tuple(row)[self.index]
		return self.function(cell)
	
	def __str(self):
		return '%d: %s' % (self.index, self.function.code)
	
	def __repr__(self):
		return self.__str()
	
	def __str__(self):
		return self.__str()

class Lambda:
	def __init__(self, code):
		self.code = code
		self.function = eval(self.code)

	def __call__(self, arg):
		return self.function(arg)
